﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;

namespace Tesla.Content {
    /// <summary>
    /// External reference represents an ISavable object that is external to another, e.g. a shared resource such as a
    /// Texture that is used for multiple materials and is written to its own file that is referenced.
    /// </summary>
    public sealed class ExternalReference : ISavable {
        private String _fileName;
        private LoaderParameters _loaderParams;

        /// <summary>
        /// Gets or sets the reference file name.
        /// </summary>
        public String FileName {
            get {
                return _fileName;
            }
            set {
                if(String.IsNullOrEmpty(value)) {
                    throw new ArgumentNullException("Reference file name cannot be null!");
                }
                _fileName = value;
            }
        }

        /// <summary>
        /// Gets or sets the loader parameters dictating optional parameters required to load the resource. If
        /// no parameters are specified, LoaderParameters.None is the default.
        /// </summary>
        public LoaderParameters LoaderParameters {
            get {
                return _loaderParams;
            }
            set {
                if(value == null) {
                    _loaderParams = LoaderParameters.None;
                } else {
                    _loaderParams = value;
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalReference"/> class.
        /// </summary>
        public ExternalReference() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalReference"/> class.
        /// </summary>
        /// <param name="fileName">The reference file name.</param>
        public ExternalReference(String fileName) : this(fileName, LoaderParameters.None) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalReference"/> class.
        /// </summary>
        /// <param name="fileName">The reference file name.</param>
        /// <param name="loaderParams">Loader parameters specifying optional parameters for loading the resource.</param>
        public ExternalReference(String fileName, LoaderParameters loaderParams) {
            if(String.IsNullOrEmpty(fileName)) {
                throw new ArgumentNullException("Reference file name cannot be null!");
            }
            _fileName = fileName;
            if(loaderParams == null) {
                _loaderParams = LoaderParameters.None;
            } else {
                _loaderParams = loaderParams;
            }
        }

        /// <summary>
        /// Serializes the object and writes it to the output.
        /// </summary>
        /// <param name="output">Savable Output</param>
        public void Write(ISavableWriter output) {
            output.Write("ExternalReference", _fileName);
            output.WriteSavable<LoaderParameters>("LoaderParameters", _loaderParams);
        }

        /// <summary>
        /// Deserializes the object and populates it from the input.
        /// </summary>
        /// <param name="input">Savable input</param>
        public void Read(ISavableReader input) {
            _fileName = input.ReadString();
            _loaderParams = input.ReadSavable<LoaderParameters>();
        }
    }
}
